<template>
  <div class="ColumnManagement">
    <div class="ColumnManagement_title">
      <el-button type="primary" @click="addColumn" size="small">添加栏目管理</el-button>
      <el-form size="small" label-width="100px" :inline="true">
        <el-form-item label="栏目标题：">
          <el-input v-model="tableFrom.name" @keyup.enter.native="getList(1)" placeholder="请输入栏目标题" class="selWidth"
            size="small">
            <el-button slot="append" icon="el-icon-search" size="small" class="el-button-solt" @click="getList(1)" />
          </el-input>
        </el-form-item>
      </el-form>

    </div>

    <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="small">
      <el-table-column prop="id" label="栏目id" width="100">
      </el-table-column>
      <el-table-column prop="name" label="栏目名称" width="180">
      </el-table-column>
      <el-table-column prop="type" label="栏目标识" width="180">
      </el-table-column>
      <el-table-column prop="cover" label="栏目封面图">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image style="width: 36px; height: 36px" :src="scope.row.cover" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="cover" label="icon">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image style="width: 36px; height: 36px" :src="scope.row.icon" />
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" active-text="显示" inactive-text="不显示"
            @change="onchangeIsShow(scope.row)" />

        </template>

      </el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" min-width="100" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateAdd(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="removeAdd(scope.row.id)"
            v-if="scope.row.enterprise_id > 0">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
        layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
        @current-change="pageChange" />
    </div>
    <AddUpData :addTitle="addTitle" :dialogVisible="dialogVisible" @dialogVisibleClick="dialogVisibleClick"
      :upDataId="upDataId"></AddUpData>
  </div>
</template>

<script>
import AddUpData from './components/add'
import { columnList, columnDelete, columnStatus } from '@/api/column'
export default {
  name: 'MerchantClassify',
  components: {
    AddUpData
  },
  data() {
    return {
      tableData: {
        data: [],
        total: 0
      },
      listLoading: true,
      dialogVisible: false,
      addTitle: '添加栏目管理',
      tableFrom: {
        page: 1,
        limit: 20,
        name: ''
      },

      upDataId: {}
    }
  },

  mounted() {
    this.getList('')
  },
  methods: {
    onchangeIsShow(row) {
      console.log(row);
      columnStatus(row).then(res => {
        console.log(res, '修改状态');
        if (res.status == 200) {
          this.$message({
            message: '状态' + res.message,
            type: 'success'
          })
        }
      })
    },
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num ? num : this.tableFrom.page
      columnList(this.tableFrom).then((res) => {
        console.log(res);
        if (res.status === 200) {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        }
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList('')
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList('')
    },
    handleClick(row) {
      console.log(row);
    },
    addColumn() {
      this.dialogVisible = true;
    },
    updateAdd(row) {
      this.addTitle = '编辑栏目管理'
      this.upDataId = {
        id: row.id,

      };
      this.dialogVisible = true;
    },
    dialogVisibleClick(row) {
      console.log(row, 'row');
      this.dialogVisible = row;
      this.getList('');
    },
    removeAdd(id) {
      columnDelete(id).then((res) => {
        console.log(res);
        if (res.status === 200) {
          this.$message({
            message: res.message,
            type: 'success'
          });
          this.getList('');
        }
      }).catch(res => {
        this.$message.error(res.message)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}

.ColumnManagement {
  padding: 20px 40px;
  box-sizing: border-box;

  .ColumnManagement_title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>
